/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TelaTipoDeServiço.java
 *
 * Created on 07/11/2010, 11:58:06
 */
package asfabdesk.apresentacao;

import asfabdesk.dominio.TipoServico;
import asfabdesk.presistencia.DAO;
import com.lowagie.tools.plugins.Txt2Pdf;
import javax.swing.JOptionPane;

/**
 *
 * @author Mathias
 */
public class TelaTipoDeServico extends javax.swing.JInternalFrame {

    /** Creates new form TelaTipoDeServiço */
    public TelaTipoDeServico() {
        initComponents();
    }

    public void salvar() {
        if (verificarCampo()) {
            if (verificarDup()) {
                try {
                    TipoServico tipo = new TipoServico(txtServico.getText().toUpperCase(), true, false);
                    new DAO().salvar(tipo);
                    JOptionPane.showMessageDialog(null, "Cadastro efetuado com Sucesso'.", "Sucesso!",1);
                    limpar();
                } catch (Exception ex) {
                    ex.printStackTrace();
                    JOptionPane.showMessageDialog(null, "Ops, ocorreu um erro durante o cadastro'.", "Ops,erro!", 0);
                }

            }
        }
    }

    public boolean verificarCampo() {
        boolean retorno = true;
        if (txtServico.getText().equals("")) {
            retorno = false;
        }
        if (retorno == false) {
            JOptionPane.showMessageDialog(null, "Ops, você não preencheu o campo obrigatório", "Campos Obrigatórios.", 2);
        }
        return retorno;
    }

    public boolean verificarDup() {
        boolean r = true;
        if (new DAO().localizar(TipoServico.class, "TipoServicoPorNome", txtServico.getText().toUpperCase()) != null) {
            r = false;
        }
        if (r == false) {
            JOptionPane.showMessageDialog(null, "Ops, você está tentando salvar um Tipo de Serviço já cadastrado", "Ops.", 2);
        }
        return r;
    }

    public void limpar() {
        txtServico.setText("");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtServico = new javax.swing.JTextField();
        btnNovo = new javax.swing.JButton();
        btnAdicionar = new javax.swing.JButton();

        setClosable(true);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(asfabdesk.apresentacao.AsfabDeskApp.class).getContext().getResourceMap(TelaTipoDeServico.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jPanel1.setBackground(resourceMap.getColor("jPanel1.background")); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel2.setBackground(resourceMap.getColor("jPanel2.background")); // NOI18N
        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setIcon(resourceMap.getIcon("jLabel1.icon")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        jPanel2.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 0, -1, -1));

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N
        jPanel2.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, -1, -1));

        txtServico.setText(resourceMap.getString("txtServico.text")); // NOI18N
        txtServico.setName("txtServico"); // NOI18N
        jPanel2.add(txtServico, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, 320, -1));

        btnNovo.setText(resourceMap.getString("btnNovo.text")); // NOI18N
        btnNovo.setName("btnNovo"); // NOI18N
        btnNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNovoActionPerformed(evt);
            }
        });
        jPanel2.add(btnNovo, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 60, -1, -1));

        btnAdicionar.setText(resourceMap.getString("btnAdicionar.text")); // NOI18N
        btnAdicionar.setName("btnAdicionar"); // NOI18N
        btnAdicionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAdicionarActionPerformed(evt);
            }
        });
        jPanel2.add(btnAdicionar, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 60, -1, -1));

        jPanel1.add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 11, 350, 99));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNovoActionPerformed
        limpar();
    }//GEN-LAST:event_btnNovoActionPerformed

    private void btnAdicionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAdicionarActionPerformed
        salvar();
    }//GEN-LAST:event_btnAdicionarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdicionar;
    private javax.swing.JButton btnNovo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField txtServico;
    // End of variables declaration//GEN-END:variables
}
